package dj.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.appsys.tools.PageSupport;
import dj.entity.AppCategory;
import dj.entity.AppInfo;
import dj.entity.AppVersion;
import dj.entity.DataDictionary;
import dj.service.AppInfoService;


@Controller
@RequestMapping(value="/manager/backend/app")
public class AppInfoController {
	
	@Resource
	private AppInfoService service;

	@RequestMapping(value="/list")
	public String getAppInfoList(Model model,HttpSession session,String querySoftwareName,String queryFlatformId,
				String queryCategoryLevel1,String queryCategoryLevel2,String queryCategoryLevel3,String pageIndex){
		Map<String,Object> map=new HashMap<String, Object>();
		List<DataDictionary> flatFormList=null;
		List<AppCategory> categoryLevel1List = null;//列出一级分类列表，注：二级和三级分类列表通过异步ajax获取
		List<AppCategory> categoryLevel2List = null;
		List<AppCategory> categoryLevel3List = null;
		map.put("querySoftwareName", querySoftwareName);
		if(queryFlatformId!=null&&!queryFlatformId.equals("")){
		map.put("queryFlatformId", Integer.valueOf(queryFlatformId));
		}
		if(queryCategoryLevel1!=null&&!queryCategoryLevel1.equals("")){
		map.put("queryCategoryLevel1", Integer.valueOf(queryCategoryLevel1));
		}
		if(queryCategoryLevel2!=null&&!queryCategoryLevel2.equals("")){
		map.put("queryCategoryLevel2", Integer.valueOf(queryCategoryLevel2));
		}
		if(queryCategoryLevel3!=null&&!queryCategoryLevel3.equals("")){
		map.put("queryCategoryLevel3", Integer.valueOf(queryCategoryLevel3));
		}
		Integer totalCount=service.selectAll(map);
		PageSupport pageSupport=new PageSupport();
		pageSupport.setTotalCount(totalCount);
		Integer pageNo=null;
		if(pageIndex!=null){
			pageNo=Integer.valueOf(pageIndex);
		}else{
			pageNo=1;
		}
		if(pageNo>pageSupport.getTotalPageCount()){
			pageSupport.setCurrentPageNo(pageSupport.getTotalPageCount());
		}
		if(pageNo<1){
			pageSupport.setCurrentPageNo(1);
		}else{
			pageSupport.setCurrentPageNo(pageNo);
		}
		flatFormList=getDataDictionaryList();
		categoryLevel1List=getCategoryList(null);
		map.put("pageIndex",(pageSupport.getCurrentPageNo()-1)*pageSupport.getPageSize());
		map.put("pageSize", pageSupport.getPageSize());
		List<AppInfo> appInfoList=service.selectPage(map);
		model.addAttribute("appInfoList", appInfoList);
		model.addAttribute("pages", pageSupport);
		model.addAttribute("flatFormList", flatFormList);
		model.addAttribute("categoryLevel1List", categoryLevel1List);
		model.addAttribute("appInfoList", appInfoList);
		model.addAttribute("querySoftwareName", querySoftwareName);
		model.addAttribute("queryFlatformId", queryFlatformId);
		model.addAttribute("queryCategoryLevel1", queryCategoryLevel1);
		model.addAttribute("queryCategoryLevel2", queryCategoryLevel2);
		model.addAttribute("queryCategoryLevel3", queryCategoryLevel3);
		if(queryCategoryLevel2!=null&&!queryCategoryLevel2.equals("")){
			categoryLevel2List=getCategoryList(Integer.valueOf(queryCategoryLevel2));
			model.addAttribute("categoryLevel2List", categoryLevel2List);
		}
		if(queryCategoryLevel3!=null&&!queryCategoryLevel3.equals("")){
			categoryLevel3List=getCategoryList(Integer.valueOf(queryCategoryLevel3));
			model.addAttribute("categoryLevel3List", categoryLevel3List);
		}
		return "backend/applist";
	}
	
	@RequestMapping("check")
	public String check(String vid,String aid,Model model){
		
		AppInfo appInfo=service.getAppInfo(Integer.parseInt(aid));
		AppVersion appVersion=service.getAppVersion(Integer.parseInt(vid));
		
		model.addAttribute("appInfo", appInfo);
		model.addAttribute("appVersion", appVersion);
		return "backend/appcheck";
	}
	
	@RequestMapping("checksave")
	public String checksave(String id,String status){
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("id", id);
		map.put("status", status);
		if(service.update(map)){
			return "redirect:/manager/backend/app/list";
		}
		return "backend/appcheck";
	}
	
	
	public List<DataDictionary> getDataDictionaryList(){
		return service.getDataDictionaryList();
	}
	
	public List<AppCategory> getCategoryList(Integer parentId){
		return service.getCategoryList(parentId);
	}
	
	
	@RequestMapping(value="/categorylevellist.json",method=RequestMethod.GET)
	@ResponseBody
	public List<AppCategory> getAppCategoryList (@RequestParam String pid){
		Integer parentId=null;
		if(pid!=null&&!pid.equals("")){
			parentId=Integer.parseInt(pid);
		}
		List<AppCategory> list=getCategoryList(parentId);
		 
		 return list;
	}
}
